package initcontroller

import (
	"krm-backend/controllers"
	"krm-backend/utils/logs"
	"time"

	"github.com/dotbalo/kubeutils/kubeutils"
	"k8s.io/client-go/informers"
	"k8s.io/client-go/tools/cache"
)

//dev: prod:1 , dp:2, ds:3, sts:4
//sit: prod:1 , dp:2, ds:3, sts:4
//map[string]map[string]int
//map[dev][pod]500
//        [ds]300
//map[sit][pod]500
//		  [ds]300

func clusterStatistics(clusterId, kubeconfig string) {
	logs.Info(map[string]interface{}{"集群ID": clusterId}, "集群资源统计")
	clientset, err := kubeutils.NewClientSet(kubeconfig, 10)
	if err != nil {
		logs.Warning(map[string]interface{}{"集群ID": clusterId, "msg": err.Error()}, "创建ClientSet失败")
		return
	}
	//初始化当前集群统计的map
	controllers.ClusterStatistics[clusterId] = make(map[string]int)
	//创建每种资源的监听器
	//informerFactory: informer工厂模式
	//1.创建informerFactory对象，用于创建informer对象
	informerFactory := informers.NewSharedInformerFactory(clientset, time.Second*30)
	//2.创建各类资源的informer
	//创建Node的informer，用于监听节点的事件
	nodeInformer := informerFactory.Core().V1().Nodes().Informer()
	podInformer := informerFactory.Core().V1().Pods().Informer()
	nsInformer := informerFactory.Core().V1().Namespaces().Informer()
	deploymentInformer := informerFactory.Apps().V1().Deployments().Informer()
	dsInformer := informerFactory.Apps().V1().DaemonSets().Informer()
	stsInformer := informerFactory.Apps().V1().StatefulSets().Informer()
	cronInformer := informerFactory.Batch().V1().CronJobs().Informer()
	secretInformer := informerFactory.Core().V1().Secrets().Informer()
	cmInformer := informerFactory.Core().V1().ConfigMaps().Informer()
	svcInformer := informerFactory.Core().V1().Services().Informer()
	ingressInformer := informerFactory.Networking().V1().Ingresses().Informer()
	pvInformer := informerFactory.Core().V1().PersistentVolumes().Informer()
	pvcInformer := informerFactory.Core().V1().PersistentVolumeClaims().Informer()

	//添加nodeInformer事件
	nodeInformer.AddEventHandler(cache.ResourceEventHandlerFuncs{
		AddFunc: func(obj interface{}) {
			//添加一个节点处理的逻辑
			controllers.ClusterStatistics[clusterId]["Node"] += 1
			logs.Debug(map[string]interface{}{"集群ID": clusterId, "资源类型": "Node", "总数量": controllers.ClusterStatistics[clusterId]["Node"]}, "节点添加")
		},
		DeleteFunc: func(obj interface{}) {
			//删除一个节点处理的逻辑
			controllers.ClusterStatistics[clusterId]["Node"] -= 1
			logs.Debug(map[string]interface{}{"集群ID": clusterId, "资源类型": "Node", "总数量": controllers.ClusterStatistics[clusterId]["Node"]}, "节点删除")

		},
	})
	podInformer.AddEventHandler(cache.ResourceEventHandlerFuncs{
		AddFunc: func(obj interface{}) {
			//添加一个节点处理的逻辑
			controllers.ClusterStatistics[clusterId]["Pod"] += 1
			logs.Debug(map[string]interface{}{"集群ID": clusterId, "资源类型": "Pod", "总数量": controllers.ClusterStatistics[clusterId]["Pod"]}, "Pod添加")
		},
		DeleteFunc: func(obj interface{}) {
			//删除一个节点处理的逻辑
			controllers.ClusterStatistics[clusterId]["Pod"] -= 1
			logs.Debug(map[string]interface{}{"集群ID": clusterId, "资源类型": "Pod", "总数量": controllers.ClusterStatistics[clusterId]["Pod"]}, "Pod删除")
		},
	})
	nsInformer.AddEventHandler(cache.ResourceEventHandlerFuncs{
		AddFunc: func(obj interface{}) {
			//添加一个节点处理的逻辑
			controllers.ClusterStatistics[clusterId]["Namespace"] += 1
			logs.Debug(map[string]interface{}{"集群ID": clusterId, "资源类型": "Namespace", "总数量": controllers.ClusterStatistics[clusterId]["Namespace"]}, "Namespace添加")
		},
		DeleteFunc: func(obj interface{}) {
			//删除一个节点处理的逻辑
			controllers.ClusterStatistics[clusterId]["Namespace"] -= 1
			logs.Debug(map[string]interface{}{"集群ID": clusterId, "资源类型": "Namespace", "总数量": controllers.ClusterStatistics[clusterId]["Namespace"]}, "Namespace删除")
		},
	})
	deploymentInformer.AddEventHandler(cache.ResourceEventHandlerFuncs{
		AddFunc: func(obj interface{}) {
			//添加一个节点处理的逻辑
			controllers.ClusterStatistics[clusterId]["Deployment"] += 1
			logs.Debug(map[string]interface{}{"集群ID": clusterId, "资源类型": "Deployment", "总数量": controllers.ClusterStatistics[clusterId]["Deployment"]}, "Deployment添加")
		},
		DeleteFunc: func(obj interface{}) {
			//删除一个节点处理的逻辑
			controllers.ClusterStatistics[clusterId]["Deployment"] -= 1
			logs.Debug(map[string]interface{}{"集群ID": clusterId, "资源类型": "Deployment", "总数量": controllers.ClusterStatistics[clusterId]["Deployment"]}, "Deployment删除")
		},
	})
	dsInformer.AddEventHandler(cache.ResourceEventHandlerFuncs{
		AddFunc: func(obj interface{}) {
			//添加一个节点处理的逻辑
			controllers.ClusterStatistics[clusterId]["DaemonSet"] += 1
			logs.Debug(map[string]interface{}{"集群ID": clusterId, "资源类型": "DaemonSet", "总数量": controllers.ClusterStatistics[clusterId]["DaemonSet"]}, "DaemonSet添加")
		},
		DeleteFunc: func(obj interface{}) {
			//删除一个节点处理的逻辑
			controllers.ClusterStatistics[clusterId]["DaemonSet"] -= 1
			logs.Debug(map[string]interface{}{"集群ID": clusterId, "资源类型": "DaemonSet", "总数量": controllers.ClusterStatistics[clusterId]["DaemonSet"]}, "DaemonSet删除")
		},
	})
	stsInformer.AddEventHandler(cache.ResourceEventHandlerFuncs{
		AddFunc: func(obj interface{}) {
			//添加一个节点处理的逻辑
			controllers.ClusterStatistics[clusterId]["StatefulSet"] += 1
			logs.Debug(map[string]interface{}{"集群ID": clusterId, "资源类型": "StatefulSet", "总数量": controllers.ClusterStatistics[clusterId]["StatefulSet"]}, "StatefulSet添加")
		},
		DeleteFunc: func(obj interface{}) {
			//删除一个节点处理的逻辑
			controllers.ClusterStatistics[clusterId]["StatefulSet"] -= 1
			logs.Debug(map[string]interface{}{"集群ID": clusterId, "资源类型": "StatefulSet", "总数量": controllers.ClusterStatistics[clusterId]["StatefulSet"]}, "StatefulSet删除")
		},
	})
	cronInformer.AddEventHandler(cache.ResourceEventHandlerFuncs{
		AddFunc: func(obj interface{}) {
			//添加一个节点处理的逻辑
			controllers.ClusterStatistics[clusterId]["CronJob"] += 1
			logs.Debug(map[string]interface{}{"集群ID": clusterId, "资源类型": "CronJob", "总数量": controllers.ClusterStatistics[clusterId]["CronJob"]}, "CronJob添加")
		},
		DeleteFunc: func(obj interface{}) {
			//删除一个节点处理的逻辑
			controllers.ClusterStatistics[clusterId]["CronJob"] -= 1
			logs.Debug(map[string]interface{}{"集群ID": clusterId, "资源类型": "CronJob", "总数量": controllers.ClusterStatistics[clusterId]["CronJob"]}, "CronJob删除")
		},
	})
	secretInformer.AddEventHandler(cache.ResourceEventHandlerFuncs{
		AddFunc: func(obj interface{}) {
			//添加一个节点处理的逻辑
			controllers.ClusterStatistics[clusterId]["Secret"] += 1
			logs.Debug(map[string]interface{}{"集群ID": clusterId, "资源类型": "Secret", "总数量": controllers.ClusterStatistics[clusterId]["Secret"]}, "Secret添加")
		},
		DeleteFunc: func(obj interface{}) {
			//删除一个节点处理的逻辑
			controllers.ClusterStatistics[clusterId]["Secret"] -= 1
			logs.Debug(map[string]interface{}{"集群ID": clusterId, "资源类型": "Secret", "总数量": controllers.ClusterStatistics[clusterId]["Secret"]}, "Secret删除")
		},
	})
	cmInformer.AddEventHandler(cache.ResourceEventHandlerFuncs{
		AddFunc: func(obj interface{}) {
			//添加一个节点处理的逻辑
			controllers.ClusterStatistics[clusterId]["Configmap"] += 1
			logs.Debug(map[string]interface{}{"集群ID": clusterId, "资源类型": "Configmap", "总数量": controllers.ClusterStatistics[clusterId]["Configmap"]}, "Configmap添加")
		},
		DeleteFunc: func(obj interface{}) {
			//删除一个节点处理的逻辑
			controllers.ClusterStatistics[clusterId]["Configmap"] -= 1
			logs.Debug(map[string]interface{}{"集群ID": clusterId, "资源类型": "Configmap", "总数量": controllers.ClusterStatistics[clusterId]["Configmap"]}, "Configmap删除")
		},
	})
	svcInformer.AddEventHandler(cache.ResourceEventHandlerFuncs{
		AddFunc: func(obj interface{}) {
			//添加一个节点处理的逻辑
			controllers.ClusterStatistics[clusterId]["Service"] += 1
			logs.Debug(map[string]interface{}{"集群ID": clusterId, "资源类型": "Service", "总数量": controllers.ClusterStatistics[clusterId]["Service"]}, "Service添加")
		},
		DeleteFunc: func(obj interface{}) {
			//删除一个节点处理的逻辑
			controllers.ClusterStatistics[clusterId]["Service"] -= 1
			logs.Debug(map[string]interface{}{"集群ID": clusterId, "资源类型": "Service", "总数量": controllers.ClusterStatistics[clusterId]["Service"]}, "Service删除")
		},
	})
	ingressInformer.AddEventHandler(cache.ResourceEventHandlerFuncs{
		AddFunc: func(obj interface{}) {
			//添加一个节点处理的逻辑
			controllers.ClusterStatistics[clusterId]["Ingress"] += 1
			logs.Debug(map[string]interface{}{"集群ID": clusterId, "资源类型": "Ingress", "总数量": controllers.ClusterStatistics[clusterId]["Ingress"]}, "Ingress添加")
		},
		DeleteFunc: func(obj interface{}) {
			//删除一个节点处理的逻辑
			controllers.ClusterStatistics[clusterId]["Ingress"] -= 1
			logs.Debug(map[string]interface{}{"集群ID": clusterId, "资源类型": "Ingress", "总数量": controllers.ClusterStatistics[clusterId]["Ingress"]}, "Ingress删除")
		},
	})
	pvInformer.AddEventHandler(cache.ResourceEventHandlerFuncs{
		AddFunc: func(obj interface{}) {
			//添加一个节点处理的逻辑
			controllers.ClusterStatistics[clusterId]["PV"] += 1
			logs.Debug(map[string]interface{}{"集群ID": clusterId, "资源类型": "PV", "总数量": controllers.ClusterStatistics[clusterId]["PV"]}, "PV添加")
		},
		DeleteFunc: func(obj interface{}) {
			//删除一个节点处理的逻辑
			controllers.ClusterStatistics[clusterId]["PV"] -= 1
			logs.Debug(map[string]interface{}{"集群ID": clusterId, "资源类型": "PV", "总数量": controllers.ClusterStatistics[clusterId]["PV"]}, "PV删除")
		},
	})
	pvcInformer.AddEventHandler(cache.ResourceEventHandlerFuncs{
		AddFunc: func(obj interface{}) {
			//添加一个节点处理的逻辑
			controllers.ClusterStatistics[clusterId]["PVC"] += 1
			logs.Debug(map[string]interface{}{"集群ID": clusterId, "资源类型": "PVC", "总数量": controllers.ClusterStatistics[clusterId]["PVC"]}, "PVC添加")
		},
		DeleteFunc: func(obj interface{}) {
			//删除一个节点处理的逻辑
			controllers.ClusterStatistics[clusterId]["PVC"] -= 1
			logs.Debug(map[string]interface{}{"集群ID": clusterId, "资源类型": "PVC", "总数量": controllers.ClusterStatistics[clusterId]["PVC"]}, "PVC删除")
		},
	})
	//创建我们的工厂
	//创建一个管道，用于关闭informerfactory的start方法
	stopper := make(chan struct{})
	defer close(stopper)
	controllers.FactoryStopper[clusterId] = stopper
	go informerFactory.Start(stopper)
	<-stopper
}
